package peng.yang.express.Util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import DataBaseUtils.SQLLiteHelperPhone;
import peng.yang.express.Bean.ExpressMember;
import peng.yang.express.Bean.PhoneMembers;

/**
 * Created by Administrator on 2016/7/1.
 */
public class PhoneMembersDao {
    private final String TAG = "PhoneMemberDao";
    SQLLiteHelperPhone openHelper;
    SQLiteDatabase database;
    //对expressinfo进行增删改查

    /*
    * 借助构造方法获取上下文对象()context
    * */

    public PhoneMembersDao(Context context) {
        openHelper = new SQLLiteHelperPhone(context);
        database = openHelper.getWritableDatabase();
    }

    public SQLiteDatabase getDatabase() {
        return database;
    }

    public void setDatabase(SQLiteDatabase database) {
        this.database = database;
    }

    //插入一条记录
    public long insert(PhoneMembers member) {
        SQLiteDatabase database = openHelper.getReadableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("phone", member.getPhone());
        cv.put("fileImagePath", member.getFileImagePath());
        cv.put("netImageUrl",member.getNetImageUrl());
        cv.put("expName", member.getExpName());
        cv.put("simpleName", member.getSimpleName());

        long row = database.insert("ExpressPhoneTable", null, cv);
        return row;
    }

    //    获得数据库所有的数据
    public List<PhoneMembers> getAllMembers() {
        List<PhoneMembers> memberList = new ArrayList<>();
        SQLiteDatabase database = openHelper.getReadableDatabase();
//        要查询的字段
        //String[] columns = {"netImageUrl", "expName", "phone", "simpleName","fileImagePath"};
//        调用query查询
        Cursor cursor = database.query("ExpressPhoneTable", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String netImageUrl = cursor.getString(cursor.getColumnIndex("netImageUrl"));
            String expName = cursor.getString(cursor.getColumnIndex("expName"));
            String phone = cursor.getString(cursor.getColumnIndex("phone"));
            String fileImagePath = cursor.getString(cursor.getColumnIndex("fileImagePath"));
            String simpleName = cursor.getString(cursor.getColumnIndex("simpleName"));

            PhoneMembers member = new PhoneMembers();
            member.setNetImageUrl(netImageUrl);
            member.setExpName(expName);
            member.setPhone(phone);
            member.setFileImagePath(fileImagePath);
            member.setSimpleName(simpleName);

            memberList.add(member);
        }
        return memberList;
    }

    /*
     * 查询单条数据
     * @param expressno
     * @return
     */

    public PhoneMembers queryExpress(String simpleName){
        Log.e("PhoneDao++",simpleName);
        String sql = "select * from ExpressPhoneTable where simpleName=?";
        Cursor cursor= database.rawQuery(sql,new String[]{simpleName});
        if (cursor.moveToNext()){
            // String companyname  = cursor.getString(1);

            String fileImagePath  = cursor.getString(cursor.getColumnIndex("fileImagePath"));
            String expName  = cursor.getString(cursor.getColumnIndex("expName"));
            Log.e("PhoneDao++",fileImagePath);
            PhoneMembers bean = new PhoneMembers();
            bean.setFileImagePath(fileImagePath);
            bean.setExpName(expName);
            return  bean;
        }


        return null;
    }

     /*
     * 查询单条数据
     * @param expressno
     * @return
     */

    public PhoneMembers queryExpressexpName(String expName){
        Log.e("PhoneDao++",expName);
        String sql = "select * from ExpressPhoneTable where expName=?";
        Cursor cursor= database.rawQuery(sql,new String[]{expName});
        if (cursor.moveToNext()){
            // String companyname  = cursor.getString(1);

            String fileImagePath  = cursor.getString(cursor.getColumnIndex("fileImagePath"));
            String  simpleName = cursor.getString(cursor.getColumnIndex("simpleName"));
            Log.e("PhoneDao++",fileImagePath);
            PhoneMembers bean = new PhoneMembers();
            bean.setFileImagePath(fileImagePath);
            bean.setSimpleName(simpleName);
            return  bean;
        }


        return null;
    }

   //插入数据库
    /*
    * 是对数据库中的数据进行操作，那么首先要去获得SQLiteDatabase
    * 该类是通过SQLiteOpenHelperd的对象，调用getReadable...getWriteale....
    * */


    /* public void insert(String expressno,String companyname){

      *//*  String sql = "insert into expressinfo(expressno,companyname) values('"+expressno+"','"+companyname+"')";
        database.execSQL(sql);*//*
        ContentValues values = new ContentValues();
        values.put("expressno",expressno);
        values.put("companyname",companyname);
        database.insert("expressinfo",null,values);

    }

    public void insertData(ExpressBean expressBean){
        ContentValues values = new ContentValues();
        values.put("expressno",expressBean.getExpressNo());
        values.put("companyname",expressBean.getCompanyName());
        database.insert("expressinfo",null,values);


    }







    *//**
     * 查询多条数据
     * @return
     *//*

    public List<ExpressBean> queryAllExpressBean(){
        String sql = "select * from expressinfo";
        Cursor cursor= database.rawQuery(sql,null);
        List<ExpressBean> beanList = new ArrayList<>();
        while (cursor.moveToNext()){
            ExpressBean bean = new ExpressBean();
            String name = cursor.getString(cursor.getColumnIndex("companyname"));
            bean.setCompanyName(name);
            beanList.add(bean);
        }

        return  beanList;

    }

    *//**
     * 根据快递单号删除快递信息
     * @param expressNo
     *//*

    public void delete(String expressNo){
        *//*String  sql  = "delete from expressinfo where expressno = "+expressNo;

        database.execSQL(sql);*//*
        String  where = "expressno = ?";
        database.delete("expressinfo",where,new String[]{expressNo});
    }

    public  void update(String expressNo){

       *//* String  sql = "update expressinfo set companyname = '申通' where expressno = '"+ expressNo+"'";

        database.execSQL(sql);*//*
        ContentValues values = new ContentValues();
        *//*
        companyname是表的字段的名字
        "韵达"是companyname所对应的新值
        * *//*
        values.put("companyname","韵达");
        *//*
        where指定条件
        new String[]{expressNo}：提供条件满足的值
        * *//*
        String  where = " expressno =?";
        database.update("expressinfo",values,where,new String[]{expressNo});


    }

    public void updateData(String expressNo,ContentValues values){
        String  where = " expressno =?";
        database.update("expressinfo",values,where,new String[]{expressNo});


    }*/
}
